package com.pj.point.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;

import com.pj.point.R;
import com.pj.point.adapter.PointListAdapter;
import com.pj.point.litepal.PointJavaBean;
import com.pj.point.point.NewPoint;
import com.pj.point.util.alert.AlertUtil;

import org.litepal.LitePal;

import java.util.List;


public class PointFragment extends Fragment {

    Activity activity;
    private ListView pointList;
    private CardView addPoint;
    private List<PointJavaBean> all;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = getActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_point, container, false);
        initView(view);
        getData();
        return view;
    }

    private void getData() {
         all = LitePal.findAll(PointJavaBean.class);
         if (all != null && all.size() > 0){
             pointList.setAdapter(new PointListAdapter(all));
             pointList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                 @Override
                 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                     if (all.get(position).getState() != 1) return;
                     Intent intent = new Intent(activity, NewPoint.class);
                     intent.putExtra("pointId",all.get(position).getPointId());
                     startActivityForResult(intent,1);
                 }
             });

             pointList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
                 @Override
                 public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                     AlertUtil.show(activity,"取消通知提醒","确定要取消通知吗",b -> {
                         if (b){
                             PointJavaBean bean = all.get(position);
                             bean.delete();
                             all.remove(position);
                             getData();
                         }
                     });
                     return true;
                 }
             });
         }else{
             pointList.setAdapter(new PointListAdapter(all));
             Toast.makeText(activity, "暂无通知和提醒", Toast.LENGTH_SHORT).show();
         }
    }

    private void initView(View view) {
        pointList = (ListView) view.findViewById(R.id.point_list);
        addPoint = (CardView) view.findViewById(R.id.add_point);

        addPoint.setOnClickListener(v -> {
            startActivityForResult(new Intent(activity, NewPoint.class),2);
        });
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == -1 && requestCode == 2){
            getData();
        }
    }
}